package com.hzt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzt.system.common.Constant;
import com.hzt.system.common.DataGridView;
import com.hzt.system.common.ResultObj;
import com.hzt.system.service.UserService;
import com.hzt.system.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzt.system.mapper.RoleMapper;
import com.hzt.system.domain.Role;
import com.hzt.system.service.RoleService;
import org.springframework.web.bind.annotation.RequestMapping;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public DataGridView loadAllRole(RoleVo roleVo) {
        IPage<Role> page=new Page<>();
        QueryWrapper<Role> qw=new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(roleVo.getName()), "name", roleVo.getName());
        qw.like(StringUtils.isNotBlank(roleVo.getRemark()), "name", roleVo.getRemark());
        qw.eq(roleVo.getAvailable()!=null, "available", roleVo.getAvailable());
        this.roleMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }


    @Override
    public DataGridView loadAllAvailableRoleNoPage(RoleVo roleVo) {
        QueryWrapper<Role> qw=new QueryWrapper<>();
        qw.eq(roleVo.getAvailable()!=null, "available", roleVo.getAvailable());
        List<Role> roles = this.roleMapper.selectList(qw);
        //根据用户id查询此用户已经拥有的角色
        List<Integer> roleIds= this.roleMapper.queryRoleIdsByUserId(roleVo.getUserId());
       List<Map<String,Object>> lists=new ArrayList<>();
        for (Role role : roles) {
            Boolean LAY_CHECKED=false;
            for (Integer roleId : roleIds) {
                if(role.getId().equals(roleId)){
                    LAY_CHECKED=true;
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("id", role.getId());
            map.put("name", role.getName());
            map.put("remark", role.getRemark());
            map.put("LAY_CHECKED", LAY_CHECKED);
            lists.add(map);
        }

        return new DataGridView(Long.valueOf(roles.size()),lists);
    }



    @Override
    public Role saveRole(Role role) {
        this.roleMapper.insert(role);
        return role;
    }

    @Override
    public Role updateRole(Role role) {
        this.roleMapper.updateById(role);
        return role;
    }

    @Override
    public List<Integer> queryMenuIdsByRid(Integer id) {
        return  this.roleMapper.queryMenuIdsByRid(id);

    }


    @Override
    public boolean removeById(Serializable id) {
        //根据角色id删除角色和菜单之间的关系
        roleMapper.deleteRoleMenuByRid(id);
//        roleMapper.deleteRoleMenuByMid(id);
        //根据角色id删除角色和用户之间的关系
        roleMapper.deleteRoleUserByRid(id);
//        roleMapper.deleteRoleUserByUid(id);

        return super.removeById(id);
    }


    /**
     * 保存角色和菜单之间的关系
     * @param rid
     * @param mids
     */
    @Override
    public void saveRoleMenu(Integer rid, Integer[] mids) {
        //先根据rid删除角色和菜单之间的关系
        this.roleMapper.deleteRoleMenuByRid(rid);
        //再保存
        if(mids!=null&&mids.length>0){
            for (Integer mid : mids) {
                this.roleMapper.insertRoleMenu(rid,mid);
            }
        }
    }


    @Override
    public List<String> queryRoleNamesByUserId(Integer id) {
        //根据用户id查询角色id的集合
        List<Integer> roleIds=this.roleMapper.queryRoleIdsByUserId(id);
        if(roleIds!=null&&roleIds.size()>0){
            QueryWrapper<Role> qw =new QueryWrapper<>();
            qw.eq("available", Constant.AVAILABLE_TRUE);
            qw.in("id", roleIds); //此id值角色id
            List<Role> roleObject = this.roleMapper.selectList(qw);
            List<String> roles=new ArrayList<>();
            for (Role role : roleObject) {
                roles.add(role.getName());
            }
            return roles;
        }else {
            return null; //授权处用null校验的
        }

    }



}
